package com.beamfield.controller.api;

import com.beamfield.common.ApiResult;
import com.beamfield.common.CommonUtils;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.modelVo.ProcNoticeWithDetailVo;
import com.beamfield.service.api.ApiProcessService;
import com.beamfield.service.manage.device.DeviceService;
import com.beamfield.service.manage.process.ProcessService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/api/process")
public class ApiProcessController {

    @Resource
    private ApiProcessService apiProcessService;

    @Resource
    private ProcessService processService;

    @Autowired
    private DeviceService deviceService;

    @RequestMapping("/data")
    @ResponseBody
    public ApiResult data(HttpServletRequest req) {
        String noticeId = req.getParameter("notice_id");
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        String beamIdStr = req.getParameter("beam_id");
        if(StringUtils.isEmpty(beamIdStr)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        long beamId = CommonUtils.parseLong(beamIdStr,0L);
        ProcNoticeWithDetailVo procNoticeWithDetailVo;
        try{
            procNoticeWithDetailVo = apiProcessService.getProcNoticeWithDetail(noticeId,beamId);
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
//        JSONObject json = new JSONObject();
//        json.put("box",procNoticeWithDetailVo);
        return new ApiResult(procNoticeWithDetailVo);
    }


    @RequestMapping("/stepData")
    @ResponseBody
    public ApiResult stepData(HttpServletRequest req) {
        String noticeId = req.getParameter("notice_id");
        if(StringUtils.isEmpty(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        String beamIdStr = req.getParameter("beam_id");
        if(StringUtils.isEmpty(beamIdStr)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        long beamId = CommonUtils.parseLong(beamIdStr,0L);
        String stepStr = req.getParameter("step");
        if(StringUtils.isEmpty(stepStr)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        int step = CommonUtils.parseInt(stepStr,0);
        try {
            switch (step) {
                //钢筋绑扎
                case 1 :return processService.getGJBZData(noticeId, null, stepStr);
                //模板安装
                case 2 :return processService.getMBAZData(noticeId, null, stepStr);
                //混凝土
                case 3 : return processService.getHNTData(noticeId, null, stepStr);
                //拆模施工
                case 4 : return processService.getCMSGData(noticeId, null, stepStr);
                //养护施工
                case 5 : return processService.getYHSGData(noticeId, null, stepStr, beamId);
                //预张拉
                case 6 : return processService.getYZLData(noticeId, null, stepStr);
                //初张拉
                case 7 : return processService.getCZLData(noticeId, null, stepStr);
                //移存梁
                case 8 : return processService.getYCLData(noticeId, null, stepStr);
                //终张拉
                case 9 : return processService.getZZLData(noticeId, null, stepStr);
                //切丝压浆
                case 10 : return processService.getQSYJData(noticeId, null, stepStr);
                //封锚
                case 11 : return processService.getFMData(noticeId, null, stepStr);
                //防水
                case 12 : return processService.getFSData(noticeId, null, stepStr);
                //架梁
                case 13 : return processService.getJLData(noticeId, null, stepStr);
                default : return new ApiResult();
            }
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
    }


    @RequestMapping("/list")
    @ResponseBody
    public ApiResult list(HttpServletRequest req) {
        String beamIdStr = req.getParameter("beam_id");
        if(CommonUtils.isEmptyString(beamIdStr)){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        Long beamId = CommonUtils.parseLong(beamIdStr,0);
        return new ApiResult();
    }

    @RequestMapping("beamInfo")
    @ResponseBody
    public  ApiResult beamInfo(HttpServletRequest request, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");
//        String bim_idStr = request.getParameter("bim_id");
        String noticeId = request.getParameter("notice_id");
        String beamId = request.getParameter("beam_id");
        if(CommonUtils.isEmptyString(noticeId)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        }
        ProcNoticeWithDetailVo procNoticeWithDetailVoByBimId;
        try {
            procNoticeWithDetailVoByBimId = apiProcessService.getProcNoticeWithDetailVoByBimId(noticeId,beamId);
        }catch (Exception e){
            e.printStackTrace();
           return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        return  new ApiResult(procNoticeWithDetailVoByBimId);
    }

    @RequestMapping("/getAllInfo")
    @ResponseBody
    public ApiResult getAllInfo(HttpServletRequest req, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin","*");

        String beamIdStr = req.getParameter("beam_id");
        if(CommonUtils.isEmptyString(beamIdStr)){
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode(),HttpResultEnum.EXCEPTION.getMessage());
        }
        Long beamId = CommonUtils.parseLong(beamIdStr,0);
        List<ProcNoticeWithDetailVo> allBeamInfo = apiProcessService.getAllBeamInfo(beamId);
        return new ApiResult(allBeamInfo);
    }
    /**
     * 根据设备id获取设备信息
     * @return
     */
    @RequestMapping(value = "/getDeviceInfoById")
    @ResponseBody
    public ApiResult getDeviceInfoById(HttpServletRequest req, HttpServletResponse response){
        response.setHeader("Access-Control-Allow-Origin","*");

        String device_id = req.getParameter("device_id");
        try {
            ApiResult apiResult =  new ApiResult(deviceService.getDeviceInfoByInboundId(Long.parseLong(device_id)));
            return apiResult;
        }catch (Exception e){
            e.printStackTrace();
            return new ApiResult(HttpResultEnum.EXCEPTION.getCode());
        }

    }

}
